package PagePackage;


import static org.testng.AssertJUnit.fail;




import org.openqa.selenium.By;
import org.openqa.selenium.ElementNotVisibleException;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.StaleElementReferenceException;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.openqa.selenium.WebDriver;







public class userRolePageClass extends internalPageClass {
	
	protected WebDriverWait wait;
	
	
	
	@FindBy (xpath="html/body/div/div[1]/div/section/div/div/ul/li[1]/a")
	public WebElement contactinfotab;
	
	
	@FindBy (xpath="html/body/div/main/section/aside/nav/ul/li[2]/a")
	public WebElement roletab;
	
	@FindBy (xpath="html/body/div/main/section/main/div/select")
	public WebElement pickaroledropdown;
	
	@FindBy (xpath="html/body/div/main/section/main/div/a")
	public WebElement addrolebutton;
	
	@FindBy (xpath="html/body/div/main/section/main/ul[1]/li/x-krossover-new-role/div/div/form/div[1]/select")
	public WebElement addsport;
	
	
/*	@FindBy (name="team")
	public WebElement chooseteamdropdown;
	*/
	@FindBy (className="btn-role-new")
	public WebElement addroleButton;
	
	
	
	
	
	@FindBy (xpath="html/body/div/main/section/main/button")
	public WebElement saveroleButton;
	
	@FindBy (xpath="html/body/div/main/section/main/ul[1]/li[1]/x-krossover-new-role/div/div/div[1]/div[1]/input")
	public WebElement BasketballIndexingBox;
	
	@FindBy (xpath="html/body/div/main/section/main/ul[1]/li[1]/x-krossover-new-role/div/div/div[1]/div[3]/input")
	public WebElement FootballIndexingBox;
	
	@FindBy (xpath="html/body/div/main/section/main/ul[1]/li[1]/x-krossover-new-role/div/div/div[1]/div[5]/input")
	public WebElement LacrosseIndexingBox;
	 
	@FindBy (xpath="html/body/div/main/section/main/ul[1]/li[1]/x-krossover-new-role/div/div/div[1]/div[2]/input")
	public WebElement BasketballQABox;
	 
	@FindBy (xpath="html/body/div/main/section/main/ul[1]/li[1]/x-krossover-new-role/div/div/div[1]/div[4]/input")
	public WebElement FootballQABox;
	 
	@FindBy (xpath="html/body/div/main/section/main/ul[1]/li[1]/x-krossover-new-role/div/div/div[1]/div[6]/input")
	public WebElement LacrosseQABox;
	
	public userContactInfoPageClass clickContactInfo(){
	contactinfotab.click();
	return MyPageFactory.getPage(driver, userContactInfoPageClass.class);
	}
	
	public userRolePageClass chooseRole(String role) {
		roletab.click(); //Click on Roles Tab
		 Select select1 = new Select(pickaroledropdown);
		 select1.selectByVisibleText(role);
		 addrolebutton.click();
		 return MyPageFactory.getPage(driver, userRolePageClass.class);
		 
		 
	}
	
	
	public userRolePageClass chooseSport(String sport) {
		//WebDriverWait wait = new WebDriverWait(driver, 60);
		//wait.until(ExpectedConditions.visibilityOf(addsport));
		
		Select select2 = new Select(addsport);
		 select2.selectByVisibleText(sport);
		 
		 return MyPageFactory.getPage(driver, userRolePageClass.class);
	}
	/*public userRolePageClass chooseTeam(String team) {
		wait = new WebDriverWait (driver, 60); 
		wait.until(visibilityOfElementLocated(By.name("team")));
		driver.manage().timeouts().implicitlyWait(300, TimeUnit.SECONDS);
		String chooseteamdropdown = driver.findElement(By.name("team")).getAttribute("name");
		Select select3 = new Select(driver.findElement(By.name(chooseteamdropdown)));
		wait.until(visibilityOfElementLocated(By.name("team")));
		 select3.selectByVisibleText(team);
		 return MyPageFactory.getPage(driver, userRolePageClass.class);
	}*/
	public userRolePageClass chooseTeam(final String team) {
		
		
		
		Boolean until = new WebDriverWait( driver, 30 ).until( new ExpectedCondition<Boolean>()
		        {
		            public Boolean apply( WebDriver webDriver )
		            {
		                try
		                {
		                	String chooseteamdropdown = driver.findElement(By.name("team")).getAttribute("name");
		            		Select select3 = new Select(driver.findElement(By.name(chooseteamdropdown)));
		            		 select3.selectByVisibleText(team);
		                	
		                  
		                }
		                catch( ElementNotVisibleException e )
		                {
		                	System.out.println( "Select failed! Try again..." );
		                    return false;
		                }
		                System.out.println( "test found!" );
		                return true;
		                
		                
		            }
		
		
		        } );
		
		
		
		 return MyPageFactory.getPage(driver, userRolePageClass.class);
	}
	public userRolePageClass addRole() {
		addroleButton.click();
		return MyPageFactory.getPage(driver, userRolePageClass.class);
	}
//	public void addAssistantCoachRole() {
//		addAssistantCoachButton.click();
//	}
	public usersPageClass saveRole() {
		saveroleButton.click();
		return MyPageFactory.getPage(driver, usersPageClass.class);
	}
	
	public teamsPage clickTeamTab() {
		teamstab.click();
		return MyPageFactory.getPage(driver, teamsPage.class);
	}
	
	public usersPageClass clickUserTab() {
		userstab.click();
		return MyPageFactory.getPage(driver, usersPageClass.class);
	}
	
	public userRolePageClass removeRole(final String userrolename) {
	Boolean until = new WebDriverWait( driver, 30 ).until( new ExpectedCondition<Boolean>()
	        {
	            public Boolean apply( WebDriver webDriver )
	            {
	                try
	                {
	                	
	                	WebElement userrole=driver.findElement( By.xpath(userrolename));
	                    
	                   userrole.findElement(By.xpath("//ancestor::li[1]/span/a[text()='Remove']")).click();
	                  
	                }
	                catch( StaleElementReferenceException e )
	                {
	                	System.out.println( "Select failed! Try again..." );
	                    return false;
	                }
	                System.out.println( "test found!" );
	                return true;
	                
	                
	            }
	        } );
	return MyPageFactory.getPage(driver, userRolePageClass.class);
	}
	
	
	
	public void assertNotPresent(WebDriver driver, String text) {
		    try {
		        
		       WebElement element2 = driver.findElement(By.xpath("//span[text()='athlete']"));
			 	element2.findElement(By.xpath(text));
		        fail("Link with text <" + text + "> is present");
		    } catch (NoSuchElementException ex) { 
		        /* do nothing, link is not present, assert is passed */ 
		    }
		}
	public boolean isOnThisPage(){
		return roletab.isDisplayed();
	}
	 @Override
		void tryToOpen() {
		 goToInternalPage2()
		 .logout();
		}
	
	
}
